package client_controllers;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.net.Socket;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JPanel;

import server_logic.CcrmServer;
import server_logic.inetCon;

import com.mysql.jdbc.Statement;

import client_gui.CampaignPatternGUI;
import client_gui.CustomerServiceGUI;
import client_gui.EmployeeLogInGUI;
import client_gui.MarketingEmployeeGUI;
import client_gui.MarketingManagerGUI;
import client_gui.SalesManGUI;
import client_logic.CcrmClient;
import client_logic.dbHandler;
import entity.Boss;
import entity.Employee;

/**
 * @class EmployeeLoginController send to DB the employee password and id and Transmits appropriate window
 * or show error.
 * 
 * @param EmployeeLogInGUI links to GUI.
 * @param Boss is used as an instance, to send objects to DB and allow changes in the lists.
 * @param Employee is for a new employee instance.
 */
public class EmployeeLoginController implements dbHandler,Serializable {

	private EmployeeLogInGUI elg;
	private Employee emp;
	private Boss b;


	public EmployeeLoginController(EmployeeLogInGUI login){
		
		this.setElg(login);
		this.getElg().get_login_button().addActionListener(new LoginListener());
		this.getElg().get_exit_button().addActionListener(new ExitListener());
		
	}
	
	/**
	 * This function create_boss Makes new Boss for sending
	 * 
	 */
	public void create_boss(){
		
			this.setEmp(new Employee());
			try {
				this.getEmp().setId(elg.get_tf_id());
				this.getEmp().setPassword(elg.get_tf_pass());
				this.setB(new Boss(emp,"search","EmployeeLoginController"));
			} catch (Exception e) {
				System.out.println("Missing fields");
			}
			
			
			if(this.getEmp().getId()==-1 || this.getEmp().getPassword().equals("")){
				getElg().getMsg().setText("Missing Fields");
				getElg().getMsg().setForeground(Color.red);
				getElg().getMsg().setVisible(true);
			}
			else Search(); 
	}
	

	@Override
	public void Add() {
		// TODO Auto-generated method stub
	
	}



	@Override
	public void Search() {
try {
			CcrmClient.inet.setCurrCon(this);
			CcrmClient.inet.sendToServer(b);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			
			System.out.println("cant send to server from employee login controller");
		}
	
	}


	/**
	 * the function HandleMsg get massage from server
	 * @param msg it item from the DB Usually comes as Boss
	 */
	public void HandleMsg(Object msg){
		this.setB((Boss) msg);
		this.setEmp((Employee) this.getB().getObj());
		if(this.getB().getMsg().equals("ok"))
		{
			this.getElg().dispose();
			emp_window();
		}
		else if(this.getB().getMsg().equals("wrong id or pass")){
			this.elg.getMsg().setText("wrong id or pass");
			this.elg.getMsg().setForeground(Color.red);
			this.elg.getMsg().setVisible(true);
			System.out.println("wrong ID or Password");
		}
		else {
			this.elg.getMsg().setText("User Already Logged");
			this.elg.getMsg().setForeground(Color.red);
			this.elg.getMsg().setVisible(true);
				System.out.println("already logged");		
		}
			
	}
	
	/**
	 * This function emp_window() Transmits the employee to appropriate window
	 * 
	 */
	public void emp_window(){
		this.getElg().getTf_id().setText("");
		this.getElg().getTf_pass().setText("");
		this.getElg().getMsg().setVisible(false);
		if(this.getEmp().getRole().equals("CustomerService")){
			CustomerServiceGUI csg= new CustomerServiceGUI(this);
		}
		else if(this.getEmp().getRole().equals("Salesman")){
			try {
				SalesManGUI smg= new SalesManGUI(this);
			} catch (Exception e) {
				System.out.println(e);
				e.printStackTrace();
			}
			
		}
		else if(this.getEmp().getRole().equals("MarketingEmployee")){
			MarketingEmployeeGUI meg = new MarketingEmployeeGUI(this);
	
		}
		else if(this.getEmp().getRole().equals("MarketingManager")){
			MarketingManagerGUI mmg = new MarketingManagerGUI(this);
	
		}
	}
	

	@Override
	public String Delete() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void update() {
		// TODO Auto-generated method stub
		
	}
	
	 public class LoginListener implements ActionListener {
         
         
         public void actionPerformed(ActionEvent ev){
 				create_boss();              
         }
	 }
	 public class ExitListener implements ActionListener {             
		 public void actionPerformed(ActionEvent ev){
        	 System.exit(1);                 
	
         	}
	 }
	public EmployeeLogInGUI getElg() {
			return elg;
		}

	public void setElg(EmployeeLogInGUI elg) {
			this.elg = elg;
		}

	public Employee getEmp() {
		return emp;
	}

	public void setEmp(Employee emp) {
		this.emp = emp;
	}

	public Boss getB() {
		return b;
	}

	public void setB(Boss b) {
		this.b = b;
	}

}
